package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @Autowired
    private EmpExprService empExprService;
    @GetMapping
    public Result page(EmpQueryParam param) {
        log.info("员工列表查询条件：{}", param);
        //定义方法
        PageResult<Emp> pageResult = empService.getPageResult(param);
        // 返回结果
        return Result.success(pageResult);
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工，员工信息：{}", emp);
        // 调用service层方法保存员工信息和员工的工作经历信息
        //1.保存员工基本信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empService.save(emp);
        //2.保存员工工作经历信息
        List<EmpExpr> exprList =emp.getExprList();
        //添加工作经历所属员工
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        empExprService.saveBatch(exprList);
        return Result.success();
    }
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("批量删除学生，{}", ids);
        // 调用service层方法批量删除员工信息
        empService.removeBatchByIds(ids);

        //删除员工工作经历
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));

        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询员工信息，id：{}", id);
        //调用Service层方法查询员工信息
        Emp emp = empService.getById(id);

        //查询员工的工作经历
        List<EmpExpr> List = empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id));//员工的工作经历集合,根据id查询到的每条工作经历

        //将查询到的工作经历集合添加到emp对象中
        emp.setExprList(List);
        return Result.success(emp);
    }

    @Transactional(rollbackFor = Exception.class)
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息，员工信息：{}", emp);
        //1.修改员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empService.updateById(emp);

        //2.删除当前员工的工作经历信息
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));

        //3.批量保存新的工作经历信息
        List<EmpExpr> exprList = emp.getExprList();

        //添加工作经历所属员工
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });

        empExprService.saveBatch(exprList);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询员工列表");
        List<Emp> empList = empService.list();

        // 调用service层方法查询员工列表
        return Result.success(empList);
    }
}
